<?php
/**
 * Created by PhpStorm.
 * User: chenyu
 * Date: 2017-09-11
 * Time: 14:01
 */

namespace EpgApp\modules\sdks\Image\base\Imagickclass;

class_exists('Imagick') or die('module Imagick not loading or not installed !');

use EpgApp\modules\sdker\Image\base\ImageException ;

class ImagickSDK
{

    private static $cache = array();

    public function __construct($filename)
    {
        $resource = new \Imagick($filename);
        if(empty($resource))
        {
            $error = 'class init falied';
            $code  = ImageException::_code($error);
            throw new ImageException(ImageException::createMsgByCode($code,__CLASS__),$code);
        }
        self::$cache['resource'] = $resource ;
        self::$cache['filename'] = $filename ;
    }

    public  function Instace()
    {
        if(empty(self::$cache))
        {
            new self(null);
        }
        else if(!empty(self::$cache['resource']))
        {
            return self::$cache['resource'] ;
        }
        $error = 'class init falied';
        $code  = ImageException::_code($error);
        throw new ImageException(ImageException::createMsgByCode($code,__CLASS__),$code);
    }

    private static function getCache($key)
    {
        if(empty($key))
        {
            return false;
        }
        if(isset(self::$cache[$key]))
        {
            return self::$cache[$key];
        }
        return false;
    }
}